package com.review.aop.common.interceptor;

import com.review.aop.common.constant.Constants;
import com.review.aop.common.util.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken = request.getHeader(Constants.USER_TOKEN_HEADER_KEY);
        if (!StringUtils.hasLength(userToken)){
            log.error("用户未登录");
            return false;
        }
        log.info("从Header中获取token："+userToken);
        Claims claims;
        try {
            claims=JwtUtil.parseToken(userToken,Constants.SECRET);
        }catch (Exception e){
            log.error("token不正确");
            return false;
        }
        return true;
    }
}
